﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Addition : Form
    {
        public Addition()
        {
            InitializeComponent();
        }

        public Addition(int id, string title, string author, string createdTime)
        {
            InitializeComponent();
            this.id = id;
            textBox1.Text = id.ToString();
            textBox2.Text = title;
            textBox3.Text = author;
            textBox4.Text = createdTime;
        }
        private int id;

        private void button1_Click(object sender, EventArgs e)
        {
            var id1 = textBox1.Text;
            var Title = textBox2.Text;
            var Author = textBox3.Text;
            var CreatedTime = textBox4.Text;
            var sup = !string.IsNullOrEmpty(Author);
            if (id > 0)
            {
                if (sup)
                {
                    var sql = string.Format("update Articles set Title='{0}',Author='{1}',CreatedTime='{2}'where Id={3}"
                        , Title, Author, CreatedTime, id);
                    var res = DbHelper.AddorUpdateOrDelete(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("更新成功！");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("更新失败！");
                    }
                }
            }
            else
            {
                if (sup)
                {
                    var sql = string.Format("insert into Articles(Id,Title,Author,CreatedTime)values({0},'{1}','{2}','{3}')"
                        , id1, Title, Author, CreatedTime);
                    var dt = DbHelper.AddorUpdateOrDelete(sql);
                    if (dt == 1)
                    {
                        MessageBox.Show("添加成功！");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败！");
                    }
                }
                else
                {
                    MessageBox.Show("文章作者不能为空！");
                }
            }
        }
    }
}
